package org.mtmi.ui.scenes;

import org.eclipse.swt.graphics.Color;

/** Color utility class */
public class CLR {
	
	/**
	 * Mix color a to b using k from 0 to 1.
	 * Caller is responsible to dispose returned color.
	 * @return a*(1-k)+b*k
	 */
	public static Color mix( Color a, Color b, float k ) {
		k = Math.max( 0, Math.min( 1, k) );
		int ra = a.getRed(), rb = b.getRed();
		int ga = a.getGreen(), gb = b.getGreen();
		int ba = a.getBlue(), bb = b.getBlue();
		float k1=1-k;
		int rs = Math.round( ra*k1 + rb*k );
		int gs = Math.round( ga*k1 + gb*k );
		int bs = Math.round( ba*k1 + bb*k );
		return new Color( a.getDevice(), rs, gs, bs );
	}

	/** 
	 * @return darker color, when k=0, return color c, when k=1, return black color.
	 * Caller is responsible to dispose returned color.
	 */
	public static Color darker( Color c, float k )
	{
		int r = (int)((1-k)*c.getRed());
		int g = (int)((1-k)*c.getGreen());
		int b = (int)((1-k)*c.getBlue());
		return new Color( c.getDevice(), r,g,b);
	}
	
	/** 
	 * @return darker color, when k=0, return color c, when k=1, return white color.
	 * Caller is responsible to dispose returned color.
	 */
	public static Color lighter( Color c, float k )
	{
		int r = (int)(k*(255-c.getRed()) + c.getRed());
		int g = (int)(k*(255-c.getGreen()) + c.getGreen());
		int b = (int)(k*(255-c.getBlue()) + c.getBlue());
		return new Color( c.getDevice(), r,g,b);
	}
}
